/*
 * 作者：姚俊
 * 功能：论文超级检索的action
 * 日期：2015-06-26
 */
package admin.action.superSearch;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import admin.action.fruits.Constant;
import admin.service.fruits.PaperService;

import teacher.bean.Paper;
import tools.StringDate;

public class PaperSuperAction {
	
	private List<Paper> papers;
	private Paper paper;
	private String date2;
	private int direction;
	//当前页
	private int currentPage;
	//最后一页
	private int endPage;
	private int counts;
	private int flag;
	private int code;
	private String type;
	private String jclass;
	private String meetClass;
	private String include;
	private String date;
	
	public List<Paper> getPapers() {
		return papers;
	}
	public void setPapers(List<Paper> papers) {
		this.papers = papers;
	}
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	public String getDate2() {
		return date2;
	}
	public void setDate2(String date2) {
		this.date2 = date2;
	}

	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public int getCounts() {
		return counts;
	}
	public void setCounts(int counts) {
		this.counts = counts;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}

	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getJclass() {
		return jclass;
	}
	public void setJclass(String jclass) {
		this.jclass = jclass;
	}
	public String getMeetClass() {
		return meetClass;
	}
	public void setMeetClass(String meetClass) {
		this.meetClass = meetClass;
	}
	public String getInclude() {
		return include;
	}
	public void setInclude(String include) {
		this.include = include;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String execute()
	{
		//定义按照高级搜索的方式进入list
		this.setFlag(4);
		if(this.getPaper()!=null)
		{
			
			if(this.getPaper().getName()==null||this.getPaper().getName().equals(""))
				this.getPaper().setName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setName(new String(this.getPaper().getName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getMeetName()==null||this.getPaper().getMeetName().equals(""))
				this.getPaper().setMeetName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setMeetName(new String(this.getPaper().getMeetName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getPubNumber()==null||this.getPaper().getPubNumber().equals(""))
				this.getPaper().setPubNumber(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setPubNumber(new String(this.getPaper().getPubNumber().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getFauthor()==null||this.getPaper().getFauthor().equals(""))
				this.getPaper().setFauthor(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setFauthor(new String(this.getPaper().getFauthor().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getOauthor()==null||this.getPaper().getOauthor().equals(""))
				this.getPaper().setOauthor(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setOauthor(new String(this.getPaper().getOauthor().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getTauthor()==null||this.getPaper().getTauthor().equals(""))
				this.getPaper().setTauthor(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setTauthor(new String(this.getPaper().getTauthor().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getPbulication()==null||this.getPaper().getPbulication().equals(""))
				this.getPaper().setPbulication(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getPaper().setPbulication(new String(this.getPaper().getPbulication().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getPaper().getDate()!=null)
				this.getPaper().setDate(
						StringDate.StringToDate(
								StringDate.DateToString(
										this.getPaper().getDate())));
			
		}
		
		System.out.println("当前的论文类型为:"+this.getPaper().getType());
		
		
		/**
		 * laohe  排序
		 * 
		 */
		List<String> orders=new ArrayList<String>();
		if(this.getType()!=null&&!(this.getType().equals("")))
		{
			orders.add(this.getType());
		}
		if(this.getJclass()!=null&&!(this.getJclass().equals("")))
		{
			orders.add(this.getJclass());
		}
		if(this.getMeetClass()!=null&&!(this.getMeetClass().equals("")))
		{
			orders.add(this.getMeetClass());
		}
		if(this.getInclude()!=null&&!(this.getInclude().equals("")))
		{
			orders.add(this.getInclude());
		}
		if(this.getDate()!=null&&!(this.getDate().equals("")))
		{
			orders.add(this.getDate());
		}
		//System.out.println("chi cun"+orders.size());
		//排序结束
		//当前页为零的时候，把当前页变成1
		if(this.getCurrentPage()==0)
			this.setCurrentPage(1);
		
		int add=0;
		//最后一页
		if(new PaperService().getPaperCountByDirection(
				this.getPaper(), this.getDate2(),this.getDirection(),Constant.state)%Constant.pageSize!=0)
		{
			add=1;
		}

		this.setEndPage(
				new PaperService().getPaperCountByDirection(
						this.getPaper(), this.getDate2(),this.getDirection(),Constant.state)/Constant.pageSize+add);
		
		this.setCounts(new PaperService().getPaperCountByDirection(
				this.getPaper(), this.getDate2(), this.getDirection(),Constant.state));
		
		
		this.setPapers(
				new PaperService().getSegregatePaperByDirection(
						this.getPaper(), this.getDate2(),this.getDirection(),(this.getCurrentPage()-1)*Constant.pageSize+1,Constant.pageSize,Constant.state,orders));

		return "succ";
	}

}
